001 package net.sf.xdc.processing;
002
003 /*
004 * Copyright 2005-2006 Jens Voß.
005 *
006 * Licensed under the GNU Lesser General Public License (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://opensource.org/licenses/lgpl-license.php
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 import java.io.File;
020 import java.util.Properties;
021 import java.util.List;
022 import java.util.Vector;
023 import java.util.Iterator;
024
025 /**
026 * This class represents an XML source file which is to be processed by the
027 * XDC tool.
028 *
029 * @author Jens Voß
030 * @since 0.5
031 * @version 0.5
032 */
033 public class XdcSource implements Comparable {
034
035 private final File file;
036 private File sourceDir;
037 private String packageName;
038 private DialectHandler dialectHandler;
039 private String rootComment;
040 private List patterns; // List<Pattern>
041
042 /**
043 * Public constructor.
044 *
045 * @param file The <code>File</code> object of this <code>XdcSource</code>
046 * @param sourceDir The source directory in whose subdirectory tree this
047 * <code>XdcSource</code> lies. Note that this does <b>not</b>
048 * necessarily mean that this <code>XdcSource</code> lies
049 * <em>directly</em> in the source directory (unless the
050 * <code>packageName</code> parameter is the empty String).
051 * @param packageName The name of the package in which this
052 * <code>XdcSource</code> lies
053 * @param handler The <code>DialectHandler</code> which controls with which
054 * stylesheet this <code>XdcSource</code> is processed
055 */
056 public XdcSource(File file, File sourceDir, String packageName,
057 DialectHandler handler) {
058 this.file = file;
059 this.sourceDir = sourceDir;
060 this.packageName = packageName;
061 dialectHandler = handler;
062 patterns = new Vector();
063 }
064
065 /**
066 * Getter method for this <code>XdcSource</code>'s <code>File</code> object.
067 *
068 * @return The <code>File</code> object of this <code>XdcSource</code>
069 */
070 public File getFile() {
071 return file;
072 }
073
074 /**
075 * Getter method for this <code>XdcSource</code>'s source directory.
076 *
077 * @return The source directory of this <code>XdcSource</code>
078 */
079 public File getSourceDir() {
080 return sourceDir;
081 }
082
083 /**
084 * Getter method for this <code>XdcSource</code>'s package name.
085 *
086 * @return The package name of this <code>XdcSource</code>
087 */
088 public String getPackageName() {
089 return packageName;
090 }
091
092 /**
093 * Getter method for this <code>XdcSource</code>'s <code>DialectHandler</code>
094 * object.
095 *
096 * @return The <code>DialectHandler</code> object of this <code>XdcSource</code>
097 */
098 public DialectHandler getDialectHandler() {
099 return dialectHandler;
100 }
101
102 /**
103 * Getter method for this <code>XdcSource</code>'s root comment String.
104 *
105 * @return The root comment String of this <code>XdcSource</code>
106 */
107 public String getRootComment() {
108 return rootComment;
109 }
110
111 /**
112 * Setter method for this <code>XdcSource</code>'s root comment String.
113 *
114 * @param rootComment The root comment String of this <code>XdcSource</code>
115 */
116 public void setRootComment(String rootComment) {
117 this.rootComment = rootComment;
118 }
119
120 /**
121 * Getter method for this <code>XdcSource</code>'s source file name.
122 *
123 * @return The source file name of this <code>XdcSource</code>
124 */
125 public String getSourceFileName() {
126 return packageName + '/' + file.getName();
127 }
128
129 /**
130 * Getter method for this <code>XdcSource</code>'s output file name.
131 *
132 * @return The output file name of this <code>XdcSource</code>
133 */
134 public String getFileName() {
135 return packageName + '/' + file.getName() + ".html";
136 }
137
138 /**
139 * This method stores an <code>XPathPattern</code> (corresponding to
140 * a link with an XPath expression anchor) in this <code>XdcSource</code>.
141 *
142 * @param pattern The <code>XPathPattern</code> to be stored in
143 * this <code>XdcSource</code>
144 */
145 public void addPattern(XPathPattern pattern) {
146 patterns.add(pattern);
147 }
148
149 /**
150 * This method retrieves all <code>XPathPattern</code> objects
151 * stored in this <code>XdcSource</code> in the form of a
152 * <code>Properties</code> object (where the links in the
153 * source file make up the keys and the <code>XPathPattern</code>
154 * values make up the values).
155 *
156 * @return A <code>Properties</code> object containing all of
157 * this <code>XdcSource</code>'s <code>XPathPattern</code>s.
158 */
159 public Properties getPatterns() {
160 Iterator iter = patterns.iterator();
161 if (!iter.hasNext()) {
162 return null;
163 }
164 Properties retVal = new Properties();
165 while (iter.hasNext()) {
166 XPathPattern pattern = (XPathPattern) iter.next();
167 String value = pattern.getValue();
168 if (value != null && value.length() > 0) {
169 retVal.setProperty(pattern.getFile() + "##" + pattern.getPattern(), value);
170 }
171 }
172 return retVal;
173 }
174
175 /**
176 * This method determines whether this <code>XdcSource</code> is equal to
177 * another such object by comparing their respective <code>File</code> objects.
178 *
179 * @param o An object this <code>XdcSource</code> is to be compared to
180 * @return <b>true</b> if this <code>XdcSource</code> and the one passed as
181 * parameter have the same <code>File</code> object; <b>false</b>
182 * otherwise
183 */
184 public boolean equals(Object o) {
185 if (this == o) {
186 return true;
187 }
188 if (o == null || getClass() != o.getClass()) {
189 return false;
190 }
191
192 final XdcSource xdcSource = (XdcSource) o;
193
194 return file.equals(xdcSource.file);
195
196 }
197
198 /**
199 * This method delegates to the <code>hashCode()</code> method of this
200 * <code>XdcSource</code>'s <code>File</code> object.
201 *
202 * @return The hash code of this <code>XdcSource</code>'s <code>File</code>
203 * object
204 */
205 public int hashCode() {
206 return file.hashCode();
207 }
208
209 /**
210 * This method is used to set file specific properties in a given
211 * <code>Properties</code> object. This object can then be passed to the
212 * XSLT stylesheet responsible for processing the XML file.
213 *
214 * @param props The <code>Properties</code> object to be modified
215 * @param prevFileName The name of the file linked via the <em>PREV FILE</em>
216 * hyperlink
217 * @param nextFileName The name of the file linked via the <em>NEXT FILE</em>
218 * hyperlink
219 */
220 public void setProcessingProperties(Properties props,
221 String prevFileName,
222 String nextFileName) {
223 props.setProperty("package", packageName);
224 props.setProperty("file", file.getName());
225 props.setProperty("outFileName", getFileName());
226 if (prevFileName != null) {
227 props.setProperty("prevFileName", prevFileName);
228 }
229 if (nextFileName != null) {
230 props.setProperty("nextFileName", nextFileName);
231 }
232 }
233
234 /**
235 * This method clears all file specific key-value pairs set in
236 * {@link #setProcessingProperties(Properties, String, String)}.
237 *
238 * @param props The <code>Properties</code> object to be modified
239 */
240 public static void clearProcessingProperties(Properties props) {
241 props.remove("package");
242 props.remove("file");
243 props.remove("outFileName");
244 props.remove("prevFileName");
245 props.remove("nextFileName");
246 }
247
248 /**
249 * This method compares this <code>XdcSource</code> with another one by<br/>
250 * (1) their file names and<br/>
251 * (2) the names of their respective packages.
252 *
253 * @param obj Another <code>XdcSource</code> object
254 * @return The result of the comparison by the above rule
255 */
256 public int compareTo(Object obj) {
257 XdcSource that = (XdcSource) obj;
258 int retVal = this.file.getName().compareTo(that.file.getName());
259 return retVal != 0 ? retVal : this.packageName.compareTo(that.packageName);
260 }
261
262 }